package gui;
import javax.swing.*;

import menus.CustMenu;

import database.CustData;
import database.OracleConnection;

import java.awt.*;
import java.awt.event.*;

public class SearchWindow extends JDialog implements ActionListener
{
	// MvbOracleConnection represents a connection to an Oracle database
	private OracleConnection mvb = OracleConnection.getInstance();



	// components of the registration window
	private JTextField categoryField = new JTextField(15);	  
	private JTextField titleField = new JTextField(15);
	private JTextField singerField = new JTextField(15);
	private JTextField quantityField = new JTextField(15);
	private JLabel categoryLabel = new JLabel("Enter category:  ");
	private JLabel titleLabel = new JLabel("Enter title:  ");
	private JLabel singerLabel = new JLabel("Enter singer:  ");
	private JLabel quantityLabel = new JLabel("Enter quantity:  ");
	private JButton searchButton = new JButton("Search");


	/*
	 * Default constructor. The registration window is constructed here.
	 */
	public SearchWindow(JFrame parent)
	{
		super(parent, "Item Search", true);
		setResizable(false);


		// content pane for the registration window
		JPanel searchPane = new JPanel();
		setContentPane(searchPane);


		/*
		 * layout components using the GridBag layout manager
		 */ 

		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();

		searchPane.setLayout(gb);
		searchPane.setBorder(BorderFactory.createEmptyBorder(22, 12, 11, 11));

		// place the username label 
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(categoryLabel, c);
		searchPane.add(categoryLabel);

		// place the text field for the username 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(categoryField, c);
		searchPane.add(categoryField);

		// place password label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(titleLabel, c);
		searchPane.add(titleLabel);

		// place the password field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(titleField, c);
		searchPane.add(titleField);

		// place name label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(singerLabel, c);
		searchPane.add(singerLabel);

		// place the name field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(singerField, c);
		searchPane.add(singerField);
		
		// place address label
		c.gridwidth = GridBagConstraints.RELATIVE;
		c.insets = new Insets(10, 10, 0, 0);
		gb.setConstraints(quantityLabel, c);
		searchPane.add(quantityLabel);

		// place the address field 
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 0, 0, 10);
		gb.setConstraints(quantityField, c);
		searchPane.add(quantityField);
		
		
		// place the register button
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.insets = new Insets(10, 10, 5, 10);
		c.anchor = GridBagConstraints.CENTER;
		gb.setConstraints(searchButton, c);
		searchPane.add(searchButton);

		// end of layout

		// Register password field and OK button with action event handler.
		// An action event is generated when the return key is pressed while 
		// the cursor is in the password field or when the OK button is pressed.
		searchButton.addActionListener(this);

		// anonymous inner class for closing the window
		addWindowListener(new WindowAdapter() 
		{
			public void windowClosing(WindowEvent e) 
			{ 
				//System.exit(0); 
			}
		});

		// initially, place the cursor in the username text field
		categoryField.requestFocus();	

		// auto-login:
		mvb.connect("ora_o4b8", String.valueOf("a29458957"));

	}


	/*
	 * event handler register button
	 */ 	    
	public void actionPerformed(ActionEvent e) 
	{
		if((categoryField.getText().trim().length() != 0) || (titleField.getText().trim().length() != 0)
				|| (singerField.getText().trim().length() != 0)){
	//		CustMenu cm = new CustMenu();
			if(Integer.valueOf(quantityField.getText().trim())!= 0){
		//		cm.showItems(categoryField.getText().trim(), titleField.getText().trim(), singerField.getText().trim(), 
	//					Integer.valueOf(quantityField.getText().trim()));
				dispose();
				
			}else{
				quantityField.setText("Enter Quantity!");
			}
			
			
			
		}
		
		
	}
}
